﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.Controllers
{
    public class SecurityController : Controller
    {
        //
        // GET: /Logon/

        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult SingIn()
        {
            return View();
        }

        [HttpPost]
        public ActionResult SingIn(string email, string password)
        {
            var edex = new EdexApiClient.EdexApiContext(email, password);

            Session.Timeout = 60;
            Session["api_session"] = edex.session;
            return RedirectToAction("Index", "Default");
        }

        [HttpGet]
        public ActionResult SingOut()
        {
            Session["api_session"] = null;
            return RedirectToAction("Index", "Default");
        }

        [HttpGet]
        public ActionResult ChangeCompany()
        {
            if (Session["api_session"] == null)
                throw new Exception("Нет доступа к данным");

            var edex = new EdexApiClient.EdexApiContext(((EdexApiClient.API_SESSION)Session["api_session"]).APIKEY);
            var data = edex.GetAllowedCompanies();

            return View(data);
        }

        [HttpPost]
        public ActionResult ChangeCompany(string company_key)
        {
            if (Session["api_session"] == null)
                throw new Exception("Нет доступа к данным");

            var edex = new EdexApiClient.EdexApiContext(((EdexApiClient.API_SESSION)Session["api_session"]).APIKEY);
            edex.ChangeCompany(company_key);

            edex = new EdexApiClient.EdexApiContext(((EdexApiClient.API_SESSION)Session["api_session"]).APIKEY);
            Session["api_session"] = edex.session;

            return RedirectToAction("Index", "Default");
        }

        [HttpGet]
        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(string email, string name)
        {
            var edex = new EdexApiClient.EdexApiContext();
            var session = edex.Register(name, email);

            Session.Timeout = 60;
            Session["api_session"] = session;

            return RedirectToAction("Index", "Default");
        }

        [HttpGet]
        public ActionResult PasswordReset()
        {
            return View();
        }

        [HttpPost]
        public ActionResult PasswordReset(string email)
        {
            var edex = new EdexApiClient.EdexApiContext();
            edex.Reset(email);

            return View("PasswordResetComplete");
        }

        /*
        [HttpGet]
        public ActionResult Profile()
        {
            var USER_GUID = ((EdexApiClient.API_SESSION)Session["api_session"]).USER_GUID;
            var edex = new EdexApiClient.EdexApiContext();

            return View();
        }
        */

        //[HttpPost]
        //public ActionResult Profile()
        //{
        //    return View();
        //} 

    }
}
